package phapp.service.common;

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;

public class SendTextMail
{
   private String mailHost = "smtp.163.com";
   private String userName = "qfdmaster";
   private String passWord = "qfd1207";
   //private String mail_head_name = "this is head of this mail";
   //private String mail_head_value = "this is head of this mail";
   private String mailTo = "459048119@qq.com";
   private String mailFrom = "qfdmaster@163.com";
   private String mailSubject = "this is the subject of this test mail";
   private String mailBody = "this is the mail_body of this test mail";
   private String personalName = "祈福灯管理员";

   public SendTextMail()
   {
   }

   public void sendMail() throws Exception
   {
      try
      {
         Properties propsSys = new Properties(); // 获取系统环境
         propsSys.put("mail.smtp.host", mailHost);
         propsSys.put("mail.smtp.auth", "true");
         Authenticator auth = new EmailAutherticator(); // 进行邮件服务器用户认证
         Session session = Session.getDefaultInstance(propsSys, auth);
         
         // 设置session,和邮件服务器进行通讯。
         MimeMessage message = new MimeMessage(session);
         // message.setContent("foobar, "application/x-foobar"); // 设置邮件格式
         
         message.setSubject(mailSubject); // 设置邮件主题
         message.setText(mailBody); // 设置邮件正文
         //message.setHeader(mail_head_name, mail_head_value); // 设置邮件标题
         message.setSentDate(new Date()); // 设置邮件发送日期
         Address address = new InternetAddress(mailFrom, personalName);
         message.setFrom(address); // 设置邮件发送者的地址
         Address toAddress = new InternetAddress(mailTo); // 设置邮件接收方的地址
         message.addRecipient(Message.RecipientType.TO, toAddress);
         Transport.send(message); // 发送邮件
         System.out.println("SendTextMail::sendMail - mailTo[" + mailTo + "]");
      }
      catch (Exception ex)
      {
         ex.printStackTrace();
         throw new Exception(ex.getMessage());
      }
   }

   public String getMailHost()
   {
      return mailHost;
   }

   public void setMailHost(String mailHost)
   {
      this.mailHost = mailHost;
   }

   public String getUserName()
   {
      return userName;
   }

   public void setUserName(String userName)
   {
      this.userName = userName;
   }

   public String getPassWord()
   {
      return passWord;
   }

   public void setPassWord(String passWord)
   {
      this.passWord = passWord;
   }

   public String getMailTo()
   {
      return mailTo;
   }

   public void setMailTo(String mailTo)
   {
      this.mailTo = mailTo;
   }

   public String getMailFrom()
   {
      return mailFrom;
   }

   public void setMailFrom(String mailFrom)
   {
      this.mailFrom = mailFrom;
   }

   public String getMailSubject()
   {
      return mailSubject;
   }

   public void setMailSubject(String mailSubject)
   {
      this.mailSubject = mailSubject;
   }

   public String getMailBody()
   {
      return mailBody;
   }

   public void setMailBody(String mailBody)
   {
      this.mailBody = mailBody;
   }

   public String getPersonalName()
   {
      return personalName;
   }

   public void setPersonalName(String personalName)
   {
      this.personalName = personalName;
   }

   /**
    * 用来进行服务器对用户的认证
    */
   public class EmailAutherticator extends Authenticator
   {
      public EmailAutherticator()
      {
         super();
      }

      public EmailAutherticator(String user, String pwd)
      {
         super();
         userName = user;
         passWord = pwd;
      }

      public PasswordAuthentication getPasswordAuthentication()
      {
         return new PasswordAuthentication(userName, passWord);
      }
   }

   public static void main(String[] args)
   {
      SendTextMail sendmail = new SendTextMail();
      try
      {
         sendmail.sendMail();
      }
      catch (Exception ex)
      {
      }
   }

}
